/**
 * 岗位管理
 */
layui.config({
  base: '/'
}).extend({
  flying: 'flying'
}).define(['index', 'flying'], function (exports) {  //先加载index模块，里面配置了全局的入口
  const $ = layui.$
      ,admin = layui.admin
      ,flying = layui.flying
      ,form = layui.form
      ,table = layui.table
      ,layer = layui.layer
      ,dropdown = layui.dropdown;

  flying.table({
    elem: '#LAY-position-table',
    url: '/system/post/page', //数据接口
    cols: [
      {type: 'numbers', fixed: 'left', title: '序号'}
      , {field: 'postId', title: '岗位编码', minWidth: 100}
      , {field: 'postName', title: '岗位名称', minWidth: 100}
      , {field: 'status', title: '状态', minWidth: 90, templet: '#statusSwitchTpl', unresize: true}
      , {field: 'updatedBy', title: '修改人', minWidth: 90}
      , {field: 'updatedTime', title: '修改时间', templet: '<div>{{ layui.util.toDateString(d.updatedTime, "yyyy-MM-dd HH:mm:ss") }}</div>',  minWidth: 170, sort: true}
      , {title: '操作', width: 160, align: 'center', fixed: 'right', toolbar: '#position-table'}
    ]
  });

  //监听工具条
  table.on('tool(LAY-position-table)', function (obj) {
    let line_data = obj.data;
    if (obj.event === 'del') {

      layer.confirm('真的删除行么？', function (index) {
        //提交 Ajax 成功后，更新表格中的数据
        admin.req({
          url: '/system/post/deleteById/' + line_data.id
          , type: 'get'
          , done: function (res) {
            //执行成功
            layer.msg(res.msg, {icon: 1});
            table.reload('LAY-position-table'); //数据刷新
            layer.close(index); //关闭弹层
          }
        });
      });
    } else if (obj.event === 'edit') {
      layer.open({
        type: 2
        , title: '修改'
        , content: '/sys/position/turn/edit?id=' + line_data.id
        , shade: 0.3
        // ,maxmin: true
        , resize: false
        , area: ['500px', '540px']
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
          let iframeWindow = window['layui-layer-iframe' + index]
              , submitID = 'LAY-position-front-submit'
              , submit = layero.find('iframe').contents().find('#' + submitID);

          //监听提交
          iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
            let field = data.field; //获取提交的字段

            field.id = line_data.id;

            //提交 Ajax 成功后，更新表格中的数据
            admin.req({
              url: '/system/post/save'
              , type: 'post'
              , contentType: 'application/json;charset=utf-8'
              , data: JSON.stringify(field)
              , done: function (res) {
                //执行成功
                layer.msg(res.msg, {icon: 1});
                table.reload('LAY-position-table'); //数据刷新
                layer.close(index); //关闭弹层
              }
            });
          });

          submit.trigger('click');
        }
        //打开弹层成功后的回调函数
        , success: function (layero, index) {

        }
      });
    }
  });

  //对外暴露的接口
  exports('position', {});
});